var n;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var BundleConfig = require("AA_BundleConfig");
var LogManager = require("AA_LogManager");
var SingletonManager = require("AA_SingletonManager");
var ResKeeper = require("AA_ResKeeper");
var ResLoader = require("AA_ResLoader");
var ResUtil = require("AA_ResUtil");
var UIView = require("AA_UIView");
var f = function (e) {
  function t() {
    var t = null !== e && e.apply(this, arguments) || this;
    t._isOpening = false;
    t._openQueue = [];
    t._uiConf = new Map();
    t._uiList = new Map();
    return t;
  }
  __extends(t, e);
  t.prototype.open = function (e, t) {
    var o = this;
    var n = [];
    for (var i = 2; i < arguments.length; i++) {
      n[i - 2] = arguments[i];
    }
    var a = this._uiConf.get(e);
    if (a) {
      var r = a[t];
      if (r) {
        r.args = n;
        if (this._isOpening) {
          LogManager.LogMgr.warn("正在打开其他界面," + t + "加入等待队列");
          var c = {
            bundleName: e,
            uiName: t,
            uiInfo: r
          };
          this._openQueue.push(c);
        } else {
          var u = this._uiList.get(e + "_" + t);
          if (cc.isValid(u, true)) {
            var l = u.node.zIndex;
            this._uiList.forEach(function (e) {
              e.node.zIndex > l && o.close(e);
            });
            return void u.onResume.apply(u, n);
          }
          this._show(e, t, r);
        }
      } else {
        LogManager.LogMgr.warn("Bundle:" + e + " UI:" + t + " 未注册 UI配置");
      }
    } else {
      LogManager.LogMgr.warn("Bundle:" + e + " 未注册 UI配置");
    }
  };
  t.prototype.close = function (e) {
    var t = e.bundleName;
    var o = e.uiName;
    e.showHideViews();
    e.rmEvents();
    e.onClose();
    this._uiList.delete(t + "_" + o);
    if (!e.isCache) {
      var n = e.node.getComponentsInChildren(ResKeeper.default);
      for (var i = 0; i < n.length; i++) {
        n[i].node.destroy();
      }
    }
    e.node.removeFromParent(true);
  };
  t.prototype.closeView = function (e, t) {
    var o = e + "_" + t;
    var n = this._uiList.get(o);
    if (n) {
      this.close(n);
    } else {
      LogManager.LogMgr.warn("UIManager close view error: bundle: " + e + ", view: " + n);
    }
  };
  t.prototype.closeAll = function () {
    var e = this;
    this._uiList.forEach(function (t) {
      e.close(t);
    });
  };
  t.prototype.register = function (e, t) {
    if (this._uiConf.has(e)) {
      LogManager.LogMgr.warn("Bundle:" + e + " 重复注册 UI配置");
      console.log('--------------------')
      const newE = { ...t, ...this._uiConf.get(e) }

      this._uiConf.set(e, newE);
      console.log('uie = ', this._uiConf.get(e), e);
    } else {
      this._uiConf.set(e, t);
    }
  };

  t.prototype.unRegister = function (e) {
    this._uiConf.delete(e);
  };
  t.prototype.unRegisterAll = function () {
    this._uiConf.clear();
  };
  t.prototype.getViewByName = function (e, t) {
    var o = e + "_" + t;
    return this._uiList.get(o);
  };
  t.prototype.getAllViews = function () {
    return Array.from(this._uiList.values());
  };
  t.prototype._show = function (e, t, o) {
    return __awaiter(this, undefined, undefined, function () {
      var n;
      var i;
      var a;
      var u;
      var l;
      var f;
      var g;
      var _;
      var m;
      return __generator(this, function (r) {
        switch (r.label) {
          case 0:
            this._isOpening = true;
            n = null;
            if (o.block) {
              n = this._getBlockInputNode(e, t);
              if (o.mask) {
                (i = n.addComponent(cc.Sprite)).sizeMode = cc.Sprite.SizeMode.CUSTOM;
                ResLoader.default.loadRes(BundleConfig.BundleNames.Resouce, "res/texture/singleColor", cc.SpriteFrame).then(function (e) {
                  i.spriteFrame = e;
                });
                n.color = cc.color(0, 0, 0);
                n.opacity = 230;
              }
            }
            return [4, ResLoader.default.loadBundle(e)];
          case 1:
            if (a = r.sent()) {
              u = o.prefab;
              return [4, ResLoader.default.loadRes(a, u, cc.Prefab)];
            } else {
              LogManager.LogMgr.error("Unable to find bundle " + e);
              return [2, this._autoExecNext(n)];
            }
          case 2:
            if (l = r.sent()) {
              f = cc.director.getScene().getChildByName("Canvas");
              g = f.getChildByName("UIRoot");
              _ = ResUtil.ResUtil.instantiate(l);
              m = _.getComponent(UIView.default);
              g.addChild(_);
              this._adaptiveCenter(_);
              _.zIndex = this._uiList.size + 1;
              _.name = e + "_" + t;
              if (m) {
                this._autoExecNext(n, _);
                m.init(e, t, o.args);
                this._uiList.set(e + "_" + t, m);
                LogManager.LogMgr.debug("UIManager open view: " + t);
                return [2];
              } else {
                LogManager.LogMgr.warn("UI:" + t + " 未注册 UIView");
                return [2, this._autoExecNext(n)];
              }
            } else {
              LogManager.LogMgr.error("Unable to find bundle " + e + " prefab " + u);
              return [2, this._autoExecNext(n)];
            }
        }
      });
    });
  };
  t.prototype._autoExecNext = function (e, t) {
    this._isOpening = false;
    if (t && e) {
      e.parent = t;
      e.zIndex = -1;
      e.setPosition(cc.v2(0, 0));
    } else {
      null == e || e.removeFromParent();
    }
    if (!(this._openQueue.length <= 0)) {
      var o = this._openQueue.shift();
      this._show(o.bundleName, o.uiName, o.uiInfo);
    }
  };
  t.prototype._getBlockInputNode = function (e, t) {
    var o = new cc.Node(e + "_" + t + "_block");
    o.setContentSize(cc.winSize);
    o.addComponent(cc.BlockInputEvents);
    cc.director.getScene().getChildByName("Canvas").getChildByName("UIBlock").addChild(o);
    o.zIndex = 999;
    return o;
  };
  t.prototype._adaptiveCenter = function () { };
  return t;
}(SingletonManager.SingletonMgr);
exports.default = f.getInstance();